package fons.model.query.fun;

import java.util.Collection;
import java.util.List;

import com.google.common.collect.Lists;

public class ListFun implements Function {
	private final List<Function> list;
	
	public ListFun () {
		list = Lists.newArrayList();
	}
	
	public ListFun(List<Function> list) {
		this.list = list;
	}
	
	public List<? extends Function> get() {
		return list;
	}
	
	@Override
	public Function apply(Function other) {
		List<Function> newList = Lists.newLinkedList(list);
		newList.add(other.apply());
		return new ListFun(newList);
	}

	@Override
	public Function apply() {
		return this;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append('(');
		boolean firstTime = true;
		for (Function fun: list) {
			if (!firstTime) builder.append(", ");
			builder.append(fun);
			firstTime = false;
		}
		builder.append(')');
		return builder.toString();
	}

	public Collection<? extends Number> getList(Class<? extends Number> clazz) {
		List<Number> values = Lists.newLinkedList();
		for (Function fun: this.list) {
			values.add(clazz.cast(fun));
		}
		return values;
	}

}
